package com.xju.subjectsystem.controller;

import com.xju.subjectsystem.pojo.Result;
import com.xju.subjectsystem.pojo.Student;
import com.xju.subjectsystem.service.StudentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.controller
 * @className: StudentController
 * @author: Eiard
 * @description: TODO : 学生端
 * @date: 2024-12-16 15:42
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * @methodName: selectByUsername
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 查询个人信息（抹除密码）
     * @author: Eiard
     * @date: 2024-12-16 15:47
     * @param: username
     * @return: Result
     */
    @GetMapping("/selectByUsername")
    public Result selectByUsername(String username) {
        Student student = studentService.selectByUsername(username);

        if (student == null) {
            return Result.error("用户名不存在");
        } else {
            return Result.success(student);
        }

    }

    /**
     * @methodName: deleteByUsername
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 逻辑删除账号
     * @author: Eiard
     * @date: 2024-12-16 15:50
     * @param: username
     * @return: Result
     */
    @DeleteMapping("/deleteByUsername")
    public Result deleteByUsername(String username) {

        try {
            studentService.deleteByUsername(username);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败，格式有误");
        }

    }


    /**
     * @methodName: add
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 批量导入学生账号
     * @author: Eiard
     * @date: 2024-12-16 15:52
     * @param: students
     * @return: Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody List<Student> students) {
        Map<String, List<String>> resultData = studentService.batchAddStudents(students);

        List<String> successList = resultData.get("successList");
        List<String> failList = resultData.get("failList");

        if (successList.isEmpty()) {
            return Result.error("全部导入失败", resultData);
        } else if (!failList.isEmpty()) {
            return Result.success("部分导入成功", resultData);
        } else {
            return Result.success("全部导入成功", resultData);
        }

    }


    /**
     * @methodName: update
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 更新密码、手机号、邮箱、微信、头像、地址
     * @author: Eiard
     * @date: 2024-12-16 15:56
     * @param: student
     * @return: Result
     */
    @PutMapping("/update")
    public Result update(@RequestBody Student student) {
        try {
            studentService.updateStudent(student);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }

}
